﻿using System.Web.Mvc;
using System.Web.Routing;
using Castle.ActiveRecord.Framework;
using Castle.ActiveRecord.Framework.Config;
using Cooking.Core.Model;
using Cooking.Web.Code;

namespace Cooking.Web
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute("TipByName", "Tip/[name]", new {controller = "Tip", action = "ByName"});

            routes.MapRoute(
                "Default",                                              // Route name
                "{controller}/{action}/{id}",                           // URL with parameters
                new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
            );

        }

        protected void Application_Start()
        {
            RegisterRoutes(RouteTable.Routes);

            //Data access
            InitializeActiveRecord();

            //IoC
            InitializeStructureMap();
        }

        private void InitializeStructureMap()
        {
            Bootstrapper.ConfigureStructureMap();
            ControllerBuilder.Current.SetControllerFactory(new StructureMapControllerFactory());
        }


        private static void InitializeActiveRecord()
        {
            IConfigurationSource source = ActiveRecordSectionHandler.Instance;
            Castle.ActiveRecord.ActiveRecordStarter.Initialize(typeof (Ingredient).Assembly, source);
            
            //YOU WILL LOSE DATA!
            //Castle.ActiveRecord.ActiveRecordStarter.DropSchema();
            //Castle.ActiveRecord.ActiveRecordStarter.CreateSchema();
        }
    }
}